﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryManagementSystem
{
    public partial class UserInterface : Form
    {
        public UserInterface()
        {
            InitializeComponent();
        }

        private void UserInterface_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetData(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void button1_Click(object sender, EventArgs e)      //查询
        {
            var Title = textBox1.Text;
            var sql = string.Format("select * from Articles where Title like '%{0}%'", Title);
            var dt = DbHelper.GetData(sql);
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)      //添加
        {
            Addwindow addwindow = new Addwindow();
            var result3 = addwindow.ShowDialog();
            if (result3 == DialogResult.Yes)
            {
                var up = textBox1.Text;
                var upstment = string.Format("select * from Articles where Author like '%{0}%'", up);
                var dt = DbHelper.GetData(upstment);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
            
        }

        private void button3_Click(object sender, EventArgs e)      //删除
        {
            if (dataGridView1.SelectedRows.Count>0)
            {
                var input = dataGridView1.SelectedRows[0];
                var id = (int)input.Cells["Id"].Value;
                var delete = MessageBox.Show("你是否确定删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (delete.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var input2 = DbHelper.AddorUpdateOrDelete(sql);
                    if (input2 > 0)
                    {
                        var input3 = textBox1.Text;
                        var sql2 = string.Format("select * from Articles where Author like '%{0}%'", input3);
                        var dt = DbHelper.GetData(sql2);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功！");
                    }
                }

            }
            else
            {
                MessageBox.Show("请选择数据！");
            }
        }

        private void button4_Click(object sender, EventArgs e)      //更新
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime = dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                var CreatedTime2 = CreatedTime.ToString();
                Addwindow addwindow1 = new Addwindow(Id, Title, Author, CreatedTime2);
                var result = addwindow1.ShowDialog();
                if (result == DialogResult.Yes)
                {
                    var up = textBox1.Text;
                    var upstment = string.Format("select * from Articles where Author like '%{0}%'", up);
                    var dt = DbHelper.GetData(upstment);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("更新失败！");
                }
            }
            else
            {
                MessageBox.Show("请选择数据！");
            }
             

            //Addwindow addwindow = new Addwindow();      //跳转更新界面
            //addwindow.Show();
        }
    }
}
